﻿using System.Collections.Generic;
using System.Security.Claims;
using MongoDB.Bson;

namespace AspNet.Identity.MongoDB
{
    /// <summary>
    /// Class that represents the UserRoles table in the MySQL Database
    /// </summary>
    public class UserRolesTable
    {
        private MongoDbDatabase _database;

        /// <summary>
        /// Constructor that takes a MySQLDatabase instance 
        /// </summary>
        /// <param name="database"></param>
        public UserRolesTable(MongoDbDatabase database)
        {
            _database = database;
        }

        /// <summary>
        /// Returns a list of user's roles
        /// </summary>
        /// <param name="userId">The user's id</param>
        /// <returns></returns>
        public List<string> FindByUserId(string userId)
        {
            //List<string> roles = new List<string>();
            //string commandText = "Select Roles.Name from UserRoles, Roles where UserRoles.UserId = @userId and UserRoles.RoleId = Roles.Id";
            //Dictionary<string, object> parameters = new Dictionary<string, object>();
            //parameters.Add("@userId", userId);

            //var rows = _database.Query(commandText, parameters);
            //foreach(var row in rows)
            //{
            //    roles.Add(row["Name"]);
            //}

            //return roles;
            List<string> roles = new List<string>();
            List<string> rolesId = new List<string>();
            var rolesIdfilter = new BsonDocument(new Dictionary<string, object>()
            {
                {"UserId", userId}
            });
            var roleIdRows = _database.GetManyValList("Users", "UserRoles", rolesIdfilter);
            foreach (var row in roleIdRows)
            {
                rolesId.Add(row["RoleId"]);
            }

            foreach (var roleId in rolesId)
            {
                var rolesfilter = new BsonDocument(new Dictionary<string, object>()
                {
                    {"Id", roleId}
                });
                var roleName=_database.GetSingleVal("Name", "Users", "Roles", rolesfilter);
                roles.Add(roleName);
            }
            
            return roles;
        }

        /// <summary>
        /// Deletes all roles from a user in the UserRoles table
        /// </summary>
        /// <param name="userId">The user's id</param>
        /// <returns></returns>
        public int Delete(string userId)
        {
            //string commandText = "Delete from UserRoles where UserId = @userId";
            //Dictionary<string, object> parameters = new Dictionary<string, object>();
            //parameters.Add("UserId", userId);

            //return _database.Execute(commandText, parameters);

            var filter = new BsonDocument(new Dictionary<string, object>()
            {
                {"UserId", userId}
            });
            return _database.DelDocuments("Users", "UserRoles", filter);
        }

        /// <summary>
        /// Inserts a new role for a user in the UserRoles table
        /// </summary>
        /// <param name="user">The User</param>
        /// <param name="roleId">The Role's id</param>
        /// <returns></returns>
        public int Insert(IdentityUser user, string roleId)
        {
            //string commandText = "Insert into UserRoles (UserId, RoleId) values (@userId, @roleId)";
            //Dictionary<string, object> parameters = new Dictionary<string, object>();
            //parameters.Add("userId", user.Id);
            //parameters.Add("roleId", roleId);

            //return _database.Execute(commandText, parameters);

            var addDocument = new BsonDocument(new Dictionary<string, object>()
            {
                {"UserId", user.Id},
                {"RoleId", roleId},
            });
            return _database.InsertSingleDocument("Users", "UserRoles", addDocument);

        }
    }
}
